#!/usr/bin/env python
#-*- coding:utf-8 -*-

from datetime import datetime

# 时间的辅助类
class datetimeHelper(object):

    #获取当前时间
    @staticmethod
    def getNow():
        dt=datetime.now()
        return dt

    #获取当前日期
    @staticmethod
    def getDate():
        dt=datetime.now()
        return dt.date()
    
    #获取当前时间的时间戳
    @staticmethod
    def getTimestamp():
        dt=datetime.now()
        return dt.timestamp()
    
    #根据时间戳转换为时间格式 
    @staticmethod
    def getTimeFromTimestamp(stamp):
        try:
            return datetime.fromtimestamp(stamp)
        except  Exception as e:
            return Null
    
    #获取指定时间的时间戳
    @staticmethod
    def getTimestampFromDatetime(dt):
        if isinstance(dt,datetime):
            return dt.timestamp()
        else:
            return 0

    
    #字符串转换为时间
    @staticmethod
    def getDatetime(str):
        if str=='':
            return datetime.now()
        else:
            try:
                return datetime.strptime(str,'%Y-%m-%d %H:%M:%S')
            except Exception as e:
                return None

    #字符串转换为时间
    @staticmethod
    def getDatetime1(str=''):
        if str!='':
            return datetimeHelper.getDatetime(str)
        else:
            return datetimeHelper.getNow()



